import { TransformConfig } from '../types';

export class StyleTransformer {
  private config: TransformConfig;

  constructor(config: TransformConfig) {
    this.config = config;
  }

  async transformWXSS(content: string, filePath: string): Promise<string> {
    let css = content;
    
    // 转换rpx单位
    css = this.transformRpx(css);
    
    // 转换CSS变量
    if (this.config.rules.styles.transformCSSVars) {
      css = this.transformCSSVars(css);
    }
    
    // 转换微信小程序特定样式
    css = this.transformWXSSSpecific(css);
    
    return css;
  }

  private transformRpx(css: string): string {
    // 将rpx转换为rem或px
    return css.replace(/(\d+)rpx/g, (match, value) => {
      const num = parseInt(value);
      return `${num / 2}px`; // 简单的转换，实际项目中可能需要更复杂的计算
    });
  }

  private transformCSSVars(css: string): string {
    // 转换CSS变量语法
    return css.replace(/var\(--([^)]+)\)/g, 'var(--$1)');
  }

  private transformWXSSSpecific(css: string): string {
    // 转换微信小程序特定的样式属性
    const wxssMap: Record<string, string> = {
      'flex-direction': 'flex-direction',
      'justify-content': 'justify-content',
      'align-items': 'align-items',
      'flex-wrap': 'flex-wrap'
    };

    Object.entries(wxssMap).forEach(([oldProp, newProp]) => {
      const regex = new RegExp(`${oldProp}:`, 'gi');
      css = css.replace(regex, `${newProp}:`);
    });

    return css;
  }
} 